<!--添加与编辑专栏组件-->
<template>
  <div class="app-container">
    <!--<el-tabs v-model="activeTab">-->
      <!--<el-tab-pane v-for="item in tabOptions" :key="item.key" :label="item.value" :name="item.key"></el-tab-pane>-->
    <!--</el-tabs>-->

    <el-form :model="postForm" v-loading="formLoading" :rules="rules" ref="postForm" label-width="180px" label-position="right">
      <!-- 基础设置 -->
      <div class="font-family-h font-bold margin-t50 margin-b40"><i class="square"></i>基本信息</div>
        <el-form-item label="专栏名称：" prop="name">
          <el-input maxlength=80  style="width:420px;" v-model="postForm.name" placeholder="请输入名称，建议15个汉字以内，最多支持40个汉字"></el-input>
        </el-form-item>
        <el-form-item label="专栏价格：" prop="price">
          <el-input type="number" min="0" v-model="postForm.price" placeholder="请输入内容" class="width110"></el-input>元
        </el-form-item>
        <el-form-item label="积分可抵扣金额：" prop="deductiblePoints" v-if="creditShow">
          <el-input type="number" min="0" :max="postForm.price" v-model="postForm.deductiblePoints" placeholder="请输入积分可抵扣金额" class="width110"></el-input>元
          <span class="el-form-tip">（购买时允许积分抵扣的金额）</span>
        </el-form-item>
        <el-form-item label="vip折扣：" prop="vipDiscount">
          <el-input type="number" min="0" max="100" v-model="postForm.vipDiscount" class="width110"></el-input>%
          <span class="point el-form-tip"> 注意：会员以上级别购买才可参与折扣。票价 * 比例 = 当前价格。例如 75% 同于 7.5 折，0% 即票价免费。系统默认初始化 100%</span>
        </el-form-item>
        <el-form-item label="svip折扣：" prop="svipDiscount" v-if="this.enableSvip">
          <el-input type="number" min="0" max="100" v-model="postForm.svipDiscount" class="width110"></el-input>%<span></span>
        </el-form-item>

        <!--<el-form-item label="是否上架：">-->
          <!--<el-select v-model="postForm.status" placeholder="请选择" style="width: 650px" @change="isUpSelect">-->
            <!--<el-option v-for="item in statusOptions" :key="item.key" :label="item.value" :value="item.key"></el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
      <el-form-item label="是否上架:" prop="status">
        <el-radio-group v-model="postForm.status">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
        <!--<el-form-item label="是否更新：">-->
          <!--<el-select v-model="postForm.isUpdating" placeholder="请选择" style="width: 650px" @change="isUpdateSelect">-->
            <!--<el-option v-for="item in commonOptions" :key="item.key" :label="item.value" :value="item.key"></el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
      <el-form-item label="是否更新:" prop="isUpdating">
        <el-radio-group v-model="postForm.isUpdating">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
        <!--<el-form-item label="课程是否展示：">-->
          <!--<el-select v-model="postForm.enableShowCourses" placeholder="请选择" style="width: 650px" @change="isShowSelect">-->
            <!--<el-option v-for="item in isShowOptions" :key="item.key" :label="item.value" :value="item.key"></el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
      <el-form-item label="课程是否展示:" prop="enableShowCourses">
        <el-radio-group v-model="postForm.enableShowCourses">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
        <!--<el-form-item label="实际订阅量：">-->
          <!--<el-input type="number" v-model="postForm.subscribers" placeholder="请输入内容" class="width110" disabled></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="排序："  prop="sort">
          <el-input type="number" v-model="postForm.sort" placeholder="请输入排序" class="width110"></el-input>
          <span class="el-form-tip">（数值越大排序越前）</span>
        </el-form-item>


        <!-- <el-form-item label="专栏封面(宽180*高120)：" prop="cover">
          <el-upload
            ref="avatarUpload"
            name="avatar"
            :action="imageUploadUrl"
            list-type="picture"
            :before-upload="handleBeforeUpload"
            :on-success="handleAvatarSuccess"
            :on-remove="handleAvatarRemove"
            :file-list="avatarList"
            :limit="1"
            :auto-upload="false"
            style="width: 420px">
            <el-button slot="trigger" size="small" type="primary" @click="beforeChoose">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitAvatarUpload">上传到服务器</el-button>
          </el-upload>
        </el-form-item> -->
        <!-- <el-form-item label="专栏封面(宽180*高120)：" prop="cover">
          <image-component class="fl" :image="postForm.cover" :width="'180px'" :height="'120px'"></image-component>
          <el-button  class="fl" type="primary" icon="upload" style="bottom: 15px;margin-left: 40px;" @click="imagecropperShow=true">
            更换图片
          </el-button>
          <image-cropper :width="640" :height="426" :url="imageUploadUrl" @close='closeImageCropper'
                        @crop-upload-success="cropSuccess" langType="zh"
                        :key="imagecropperKey" v-show="imagecropperShow"></image-cropper>
        </el-form-item> -->
        <el-form-item label="专栏封面(宽180*高120)：" prop="cover">
          <div class="clearfix">
            <img class="fl margin-r" :src="postForm.cover" @error="setDefultImg" width="180" height="120" alt="">
            <el-upload
              class="fl"
              ref="avatarUpload"
              name="avatar"
              :action="imageUploadUrl"
              list-type="picture"
              :before-upload="handleBeforeUpload"
              :on-success="handleAvatarSuccess"
              :show-file-list="false"
              :auto-upload="true"
              style="width: 420px">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过3m</div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="专栏详情：" prop="columnDetail">
          <tinymce :height="350" v-model="postForm.description" style="width: 600px;float: left;"></tinymce>
        </el-form-item>
        <div class="font-family-h font-bold margin-t50 margin-b40" v-if="relaShow"><i class="square"></i>关联信息</div>
        <el-form-item label="关联资料" v-if="datumShow">
          <div class="clearfix c-width60 b-c" v-for="(item, index) in postForm.datumArr" :key="index">
            <div class="fl" >{{item.name}}</div>
            <b class="fr" @click="cancelRel(1, index)">取消关联 </b>
          </div>
          <div v-if="postForm.datumArr.length < 5">
          <el-button @click="addRelObj(1,postForm.datumArr)" >添加资料</el-button>
          <span class="margin-l10 fc">最多可添加5个</span>
          </div>
        </el-form-item>
        <el-form-item label="关联社群" v-if="socialShow">
          <div class="clearfix c-width60 b-c" v-for="(item, index) in postForm.socialArr" :key="index">
            <div class="fl c-f">{{item.name}}</div>
            <b class="fr" @click="cancelRel(2, index)">取消关联</b>
          </div>
          <div v-if="postForm.socialArr.length < 5">
          <el-button @click="addRelObj(2,postForm.socialArr)" >添加社群</el-button>
          <span class="margin-l10 fc">最多可添加5个</span>
          </div>
        </el-form-item>
        <el-form-item label="关联测评" v-if="examShow ">
          <div class="clearfix c-width60 b-c" v-for="(item, index) in postForm.examArr" :key="index">
            <div class="fl c-f">{{item.name}}</div>
            <b class="fr" @click="cancelRel(3, index)">取消关联</b>
          </div>
         <div v-if="postForm.examArr.length < 5">
          <el-button @click="addRelObj(3,postForm.examArr)" >添加测评</el-button>
          <span class="margin-l10 fc">最多可添加5个</span>
         </div>
        </el-form-item>
        <addRelated v-if="isShowAddRelated"  :columnId ="postForm.columnId"  :relType="relType"  :objData ="objData"   @saveRelated="saveRelated" @hideAddRelated="hideAddRelated"></addRelated>

      <!--分享设置-->
      <div class="font-family-h font-bold margin-t50 margin-b40"><i class="square"></i>推荐设置</div>
      <el-form-item label="是否开通邀请卡:" prop="isInvitationCard">
        <el-radio-group v-model="postForm.isInvitationCard">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="邀请后可优惠：" prop="deductibleAfterInvition" v-show="postForm.isInvitationCard">
        <el-input type="number" min="0" :max="postForm.price" v-model.number="postForm.deductibleAfterInvition" class="width110"></el-input>元
        <span class="el-form-tip">（邀请优惠和积分抵扣不会同时享受）</span>
      </el-form-item>
      <el-form-item label="虚拟起售数：" prop="initSales">
        <el-input type="number" min="0" v-model="postForm.initSales" placeholder="请输入内容" class="width110"></el-input>
        <span class="el-form-tip">前端订阅量=虚拟起售数+实际订阅量</span>
      </el-form-item>
      <!-- <el-form-item label="点击量：" prop="clicks">
        <el-input type="number" v-model="postForm.clicks" placeholder="请输入点击量" class="width110"></el-input>
      </el-form-item> -->
        <el-form-item label="分享公众号描述：" prop="shareDesc">
           <el-input :maxlength=29 v-model="postForm.shareDesc" placeholder="分享描述,不超过29个字" style="width: 600px"></el-input>
        </el-form-item>

      <!-- 抽成设置 -->
      <div class="font-family-h font-bold margin-t50 margin-b40"><i class="square"></i>抽成设置<span class="point">(以下六个抽成比例之和，不能超过100%；如果还有收益人抽成，则包括收益人的抽成之和不能超过100%)</span></div>
      <el-form-item label="是否关联收益人:">
        <el-radio-group v-model="postForm.linkedToLecturer">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
        <el-form-item label="选择收益人：" v-show="postForm.linkedToLecturer">
          <multiselect style='width:420px;' v-model="teacherInfo" :options="teacherList" track-by="userId" label="nickname"
            placeholder="搜索收益人" selectLabel="选择" deselectLabel="删除" @select="teacherOptionSelect" @search-change="syncSearchTeachers" :internalSearch="false">
            <span slot='noResult'>无结果</span>
          </multiselect>
        </el-form-item>
        <el-form-item label="收益人抽成比例(单位%)：" prop="lecturerRatio" v-show="postForm.linkedToLecturer">
          <el-input class="width110" type="number" min="0" max="100" v-model.number="postForm.lecturerRatio" placeholder="请输入讲师抽成比例" style="width: 420px"></el-input>
        </el-form-item>
        <div>
          <el-form-item label="分销抽成(单位%)：">
            <el-table :data="levelRateData" border style="width: 100%">
              <el-table-column label="第一级抽成比例">
                <template slot-scope="scope">
                  <el-form-item prop="l1Ratio">
                    <el-input type="number" min="0" max="100" style="width: 265px" v-model.number="scope.row.l1Ratio" placeholder="请输入第一级抽成比例"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="第二级抽成比例">
                <template slot-scope="scope">
                  <el-form-item prop="l2Ratio">
                    <el-input type="number" min="0" max="100" style="width: 265px" v-model.number="scope.row.l2Ratio" placeholder="请输入第二级抽成比例"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="第三级抽成比例">
                <template slot-scope="scope">
                  <el-form-item prop="l3Ratio">
                    <el-input type="number" min="0" max="100" style="width: 265px" v-model.number="scope.row.l3Ratio" placeholder="请输入第三级抽成比例"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
          <el-form-item label="服务商抽成比例(单位%)：" v-if="this.agentLevel > 0">
            <el-table :data="serverRateData" border style="width: 100%">
              <el-table-column label="代理商抽成比例">
                <template slot-scope="scope">
                  <el-form-item prop="agentRatio">
                    <el-input type="number" min="0" max="100" style="width: 265px" v-model.number="scope.row.agentRatio" placeholder="请输入代理商抽成比例"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="合伙人抽成比例" v-if="this.agentLevel == 3">
                <template slot-scope="scope">
                  <el-form-item prop="partnerRatio">
                    <el-input type="number" min="0" max="100" style="width: 265px" v-model.number="scope.row.partnerRatio" placeholder="请输入合伙人抽成比例"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="分公司抽成比例" v-if="this.agentLevel ==3">
                <template slot-scope="scope">
                  <el-form-item prop="branchRatio">
                    <el-input type="number" min="0" max="100" style="width: 265px" v-model.number="scope.row.branchRatio" placeholder="请输入分公司抽成比例"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
           <el-form-item>
          <el-button type="primary" v-loading="btnLoading" icon="el-icon-check" @click="submitForm">保存</el-button>
          <el-button type="info">取消</el-button>
        </el-form-item>
        </div>
    </el-form>
  </div>
</template>
<script>
  import addRelated from './addRelated.vue' // 引入添加管理员
  import Multiselect from 'vue-multiselect' // 引入 vue-multiselect 组件
  import 'vue-multiselect/dist/vue-multiselect.min.css'
  import { getMemberSearch, initMemberList } from '@/api/member'
  import { toGetCompanyInfo } from '@/api/common'
  import { toGetColumnInfo, toSaveColumn } from '@/api/column'
  import { enablePointModule, enableDatumModule, enableSocialModule, enableExamModule, enableSvip } from '@/utils/permission'
  import { validatePercent, validateNumber } from '@/utils/vm'
  import { getChineseStrLength } from '@/utils/validate'
  import defultImg from '@/assets/defult270.png'
  // import imageComponent from '@/components/imageComponent'
  // import ImageCropper from '@/components/ImageCropper'
  const postForm = {
    name: '', // 专栏名称
    price: 0, // 专栏价格
    status: 1, // 是否上架
    isUpdating: 1, // 是否更新
    enableShowCourses: 1, // 课程是否展示
    subscribers: 0, // 实际订阅量
    initSales: 0, // 虚拟起售数
    clicks: 0, // 点击数
    sort: 0, // 排序
    deductiblePoints: 0, // 积分可抵扣金额
    isInvitationCard: 1, // 是否开通邀请卡
    deductibleAfterInvition: 0, // 邀请后可优惠
    vipDiscount: 100, // vip折扣比例
    svipDiscount: 100, // svip折扣比例
    linkedToLecturer: 0,
    lecturerId: 0, // 选择讲师
    lecturerRatio: null, // 讲师抽成比例
    l1Ratio: 0, // 第一级抽成比例
    l2Ratio: 0, // 第二级抽成比例
    l3Ratio: 0, // 第三极抽成比例
    agentRatio: 0, // 代理商抽成比例
    partnerRatio: 0, // 合伙人抽成比例
    branchRatio: 0, // 分公司抽成比例
    columnId: 0,
    cover: '',
    datumArr: [],
    socialArr: [],
    examArr: []
  }

  const statusOptions = [ // 是否上架
    { key: 1, value: '是' },
    { key: 0, value: '否' }
  ]
  const isUpdateOptions = [ // 是否更新
    { key: 1, value: '是' },
    { key: 0, value: '否' }
  ]
  const isShowOptions = [ // 课程是否展示
    { key: 1, value: '是' },
    { key: 0, value: '否' }
  ]
  const commonOptions = [
    { key: 1, value: '是' },
    { key: 0, value: '否' }
  ]
  const teacherList = [ // 选择讲师
    { key: 0, value: '老师一啊' },
    { key: 1, value: '老师二啊' }
  ]
  const tabOptions = [ // tab标签选项
    { key: 'basic', value: '基本信息' },
    { key: 'commission', value: '抽成设置' },
    { key: 'share', value: '分享设置' }
  ]
  const levelRateData = [{ // 分销抽成
  }]
  const serverRateData = [{ // 服务商抽成
  }]

  export default {
    components: {
      addRelated,
      Tinymce: require('@/components/Tinymce').default,
      Multiselect
      // imageComponent,
      // ImageCropper
    },
    data() {
      const validateDeductibleAmount = (rule, value, callback) => {
        if (value && value < 0) {
          callback('积分抵扣金额不能小于0')
        } else {
          console.log(value, postForm.price)
          if (parseFloat(value) > parseFloat(this.postForm.price)) {
            console.log(value, postForm.price)
            callback('积分抵扣金额不能超过专栏价格')
          }
          callback()
        }
      }
      return {
        postForm: Object.assign({}, postForm),
        encodeUserId: null,
        isShowAddRelated: false,
        agentLevel: 0,
        tabOptions, // tab
        statusOptions,
        isUpdateOptions,
        isShowOptions,
        commonOptions,
        teacherList,
        formLoading: false,
        btnLoading: false,
        inviteShow: false,
        isInvite: false,
        levelRateData,
        serverRateData,
        teacherShow: false,
        courseCommissionShow: false, // 是否设置课程抽成
        isTeacher: false,
        teacherInfo: [],
        syncLoading: false,
        imageUploadUrl: process.env.BASE_API + '/common/column/imageUpload',
        avatarList: [], // 上传列表
        activeTab: 'basic',
        listLoading: false,
        relType: null,
        objData: [],
        creditShow: true, // 是否显示积分
        datumShow: true, // 资料
        socialShow: true, // 社群
        examShow: true, // 测评
        relaShow: true,
        enableSvip: true,
        rules: {
          name: [{ required: true, message: '请输入专栏名称', trigger: 'blur' },],
          cover: [{ required: true, message: '请选择图片', trigger: 'change' }],
          price: [{ required: true, trigger: 'blur', validator: validateNumber }],
          vipDiscount: [{ validator: validatePercent, trigger: 'blur' }],
          svipDiscount: [{ validator: validatePercent, trigger: 'blur' }],
          clicks: [{ validator: validateNumber, trigger: 'blur' }],
          deductiblePoints: [{ validator: validateDeductibleAmount, trigger: 'blur' }],
          sort: [{ validator: validateNumber, trigger: 'blur' }],
          initSales: [{ validator: validateNumber, trigger: 'blur' }],
          lecturerRatio: [{ validator: validatePercent, trigger: 'blur' }],
          l1Ratio: [{ validator: validatePercent, trigger: 'submitForm' }],
          l2Ratio: [{ validator: validatePercent, trigger: 'submitForm' }],
          l3Ratio: [{ validator: validatePercent, trigger: 'submitForm' }],
          agentRatio: [{ validator: validatePercent, trigger: 'submitForm' }],
          partnerRatio: [{ validator: validatePercent, trigger: 'submitForm' }],
          branchRatio: [{ validator: validatePercent, trigger: 'submitForm' }],
          shareTitle: [
            { min: 0, max: 26, message: '长度在 0 到 26 个字符', trigger: 'blur' }
          ],
          shareDesc: [
            { min: 0, max: 29, message: '长度在 0 到 29 个字符', trigger: 'blur' }
          ]
        }
        // imagecropperShow: false,
        // imagecropperKey: 0
      }
    },
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    created() {
      this.getCompanyInfo()
      this.getTeacher()
      this.enableModules()
      if (this.isEdit) {
        this.getColumnInfo()
      }
    },
    watch: {
      // 监听路由名称变化 重新调用数据
      $route: function(to, from) {
        console.log(to)
        if (to.name == 'editCourseColumn') {
          this.getCompanyInfo()
          this.getTeacher()
          this.getColumnInfo()
          this.enableModules()
        }
      }
    },
    methods: {
      enableModules() {
        if (!enablePointModule()) {
          this.creditShow = false
        }
        if (!enableDatumModule()) {
          this.datumShow = false
        }
        if (!enableSocialModule()) {
          this.socialShow = false
        }
        if (!enableExamModule()) {
          this.examShow = false
        }
        if (!enableExamModule() && !enableDatumModule() && !enableSocialModule()) {
          this.relaShow = false
        }
        if (!enableSvip()) {
          this.enableSvip = false
        }
      },
      saveRelated() {

      },
      hideAddRelated(msg) {
        this.objData.push(...msg)
        this.isShowAddRelated = false
      },
      getColumnInfo() {
        this.listLoading = true
        toGetColumnInfo(this.$route.params.columnId)
          .then(res => {
          // 正确回调
            this.postForm = res.data.data
            console.log('jdd', this.postForm)
            // 获取分销抽成比例
            this.levelRateData = [{
              l1Ratio: this.postForm.l1Ratio,
              l2Ratio: this.postForm.l2Ratio,
              l3Ratio: this.postForm.l3Ratio
            }]
            // 获取服务商抽成比例
            this.serverRateData = [{
              agentRatio: this.postForm.agentRatio,
              partnerRatio: this.postForm.partnerRatio,
              branchRatio: this.postForm.branchRatio
            }]
            if (this.postForm.isInvitationCard == 1) {
              this.inviteShow = true
            }
            if (this.postForm.linkedToLecturer == 1) {
              this.teacherShow = true
            }

            if (this.postForm.cover) {
              this.avatarList = [{ url: this.postForm.cover }]
            }

            if (this.postForm.lecturerId != null) {
              getMemberSearch(this.postForm.encodeUserId).then(res => {
                console.log('encodeUserId', this.postForm.encodeUserId)
                console.log('讲师', res)
                this.teacherInfo = res.data.data[0]
              }).catch(err => {
                console.log(err)
              })
            }

            this.listLoading = false
          })
          .catch(err => {
            console.log(err)
          })
      },
      getCompanyInfo() {
        toGetCompanyInfo(this.$route.params.columnId)
          .then(res => {
            console.log('代理', res.data.data)
            this.agentLevel = res.data.data.agentLevels
          })
          .catch(err => {
            console.log(err)
          })
      },
      isUpSelect(val) {},
      isUpdateSelect(val) {}, // 选泽是否更新
      isShowSelect(val) { console.log(val) }, // 是否展示课程
      inviteSelect(val) {
        if (val == 0) {
          this.inviteShow = false
        } else {
          this.inviteShow = true
        }
      }, // 是否开通邀请卡
      teacherSelect(val) { // 是否关联讲师
        if (val == 1) {
          this.teacherShow = true
        } else {
          this.teacherShow = false
        }
      },
      courseCommissionSelect(val) { // 是否单独设置课程抽成
        if (val == 1) {
          this.courseCommissionShow = true
        } else {
          this.courseCommissionShow = false
        }
      },
      teacherOptionSelect(selectedOption, id) { // 选择讲师
        this.postForm.lecturerId = selectedOption.userId
      },
      submitForm() {
        if (this.postForm.name == '' || this.postForm.name == null) {
          this.$message({
            type: 'warning',
            message: '专栏名称必填'
          })
          return
        }
        if(getChineseStrLength(this.postForm.name) > 80){
        this.$message({
            message: '专栏名称不能超过40个字',
            type: 'warning'
          })
          return
      }
        this.postForm.l1Ratio = this.levelRateData[0].l1Ratio ? this.levelRateData[0].l1Ratio : 0
        this.postForm.l2Ratio = this.levelRateData[0].l2Ratio ? this.levelRateData[0].l2Ratio : 0
        this.postForm.l3Ratio = this.levelRateData[0].l3Ratio ? this.levelRateData[0].l3Ratio : 0
        this.postForm.agentRatio = this.serverRateData[0].agentRatio ? this.serverRateData[0].agentRatio : 0
        this.postForm.partnerRatio = this.serverRateData[0].partnerRatio ? this.serverRateData[0].partnerRatio : 0
        this.postForm.branchRatio = this.serverRateData[0].branchRatio ? this.serverRateData[0].branchRatio : 0
        if (this.teacherInfo) {
          this.postForm.lecturerId = this.teacherInfo.userId
        }
        if (this.postForm.l1Ratio == 0) {
          if (this.postForm.l2Ratio != 0 || this.postForm.l3Ratio != 0) {
            this.$message({
              type: 'warning',
              message: '第一级别抽成设置为0，第二三级只能设置为0'
            })
            return
          }
        }
        if (this.postForm.l2Ratio == 0) {
          if (this.postForm.l3Ratio != 0) {
            this.$message({
              type: 'warning',
              message: '第二级别抽成设置为0，第三级只能设置为0'
            })
            return
          }
        }
        if (this.postForm.cover == null || this.postForm.cover == '') {
          this.$message({
            type: 'warning',
            message: '请选择图片'
          })
          return
        }

        if (this.postForm.isInvitionCard) {
          if (!this.postForm.deductibleAfterInvition) {
            this.$message({
              type: 'warning',
              message: '开通了邀请卡必须要填写邀请优惠'
            })
            return
          }
          if (this.postForm.deductibleAfterInvition > this.postForm.price) {
            this.$message({
              type: 'warning',
              message: '邀请优惠价格不能大于专栏价格'
            })
            return
          }
        }
        if (this.postForm.linkedToLecturer) {
          if (!this.teacherInfo || !this.postForm.lecturerRatio) {
            this.$message({
              type: 'warning',
              message: '选择了关联收益人必须选择收益人跟抽成比例'
            })
            return
          }
        }
        var countRate = 0
        if (this.agentLevel == 0) {
          countRate = this.postForm.l1Ratio + this.postForm.l2Ratio + this.postForm.l3Ratio
        } else if (this.agentLevel == 1) {
          countRate = this.postForm.l1Ratio + this.postForm.l2Ratio + this.postForm.l3Ratio + this.postForm.agentRatio
        } else if (this.agentLevel == 3) {
          countRate = this.postForm.l1Ratio + this.postForm.l2Ratio + this.postForm.l3Ratio + this.postForm.agentRatio + this.postForm.branchRatio + this.postForm.partnerRatio
        }
        if (this.postForm.linkedToLecturer) {
          this.postForm.lecturerRatio = this.postForm.lecturerRatio ? this.postForm.lecturerRatio : 0
          countRate += this.postForm.lecturerRatio
        }
        if (countRate > 100) {
          this.$notify({
            title: '提示',
            message: '所有分成比例之和不能超出100%!',
            type: 'warning'
          })
          return
        }
        if (this.enableSvip) {
          console.log(this.postForm.svipDiscount, this.postForm.vipDiscount)
          if (Number(this.postForm.svipDiscount) > Number(this.postForm.vipDiscount)) {
            this.$notify({
              title: '提示',
              message: 'SVIP折扣值应小于VIP（越小折扣力度越大）！',
              type: 'warning'
            })
            return
          }
        }

        this.$refs.postForm.validate(valid => {
          if (valid) {
            if (this.btnLoading) {
              return
            }
            this.btnLoading = true
            var data = { columnArr: this.postForm }
            this.formLoading = true
            toSaveColumn(data)
              .then(res => {
                this.formLoading = false
                this.btnLoading = false
                this.$notify({
                  title: '提示',
                  message: '提交成功!',
                  type: 'success'
                })
                this.close()
              })
              .catch(err => {
                this.formLoading = false
                this.btnLoading = false
                console.log(err)
              })
          }
        })
      },
      cancelRel(type, index) {
        if (type == 1) {
          this.postForm.datumArr.splice(index, 1)
          console.log(this.postForm.datumArr)
        } else if (type == 2) {
          this.postForm.socialArr.splice(index, 1)
        } else if (type == 3) {
          this.postForm.examArr.splice(index, 1)
        }
      },
      addRelObj(relType, objData) {
        this.relType = relType
        this.objData = objData
        this.isShowAddRelated = true
      },
      submitAvatarUpload() {
        this.$refs.avatarUpload.submit()
      },
      getTeacher() {
        initMemberList().then(res => {
          console.log('讲师数据', res)
          const resData = res.data.data
          this.teacherList = []
          for (let i = 0; i < resData.length; i++) {
            this.teacherList.push({
              userId: resData[i].userId,
              nickname: resData[i].nickname + `（ID: ${resData[i].encodeUserId}）`
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      syncSearchTeachers(query) { // 获取讲师数据
        getMemberSearch(query).then(res => {
          this.syncLoading = false
          const resData = res.data.data
          this.teacherList = []
          for (let i = 0; i < resData.length; i++) {
            this.teacherList.push({
              userId: resData[i].userId,
              nickname: resData[i].nickname + `（ID: ${resData[i].encodeUserId}）`
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      setDefultImg(e) {
        e.target.src = defultImg
      },
      handleAvatarSuccess(response, file, fileList) {
        if (response.statusCode == 200) {
          this.postForm.cover = response.data.imageUrl
          this.$message({
            message: '上传成功!',
            type: 'info'
          })
        } else {
          this.$message({
            message: '上传失败，请稍候再试!',
            type: 'info'
          })
        }
      },
      handleBeforeUpload(file) {
        const isImg = file.type == 'image/jpeg' || file.type == 'image/png'
        if (!isImg) {
          this.$message.warning('上传图片只能是 JPG 或 PNG 格式')
        }
        const isLt3M = file.size / 1024 / 1024 < 3
        if (!isLt3M) {
          this.$message.error('上传图片大小不能超过 3MB!')
        }
        return isImg && isLt3M
      },
      // // 图片裁剪
      // closeImageCropper() {
      //   this.imagecropperShow = false
      // },
      // cropSuccess(resData) {
      //   this.imagecropperShow = false
      //   this.imagecropperKey = this.imagecropperKey + 1
      //   this.postForm.cover = resData.data.imageUrl
      // },
      close() {
        this.$store.dispatch('delVisitedViews', this.$route).then((views) => {
          const latestView = views.slice(-1)[0]
          console.log(latestView)
          if (latestView) {
            // this.$router.push(latestView.path)
            this.$router.push({ name: 'columns' })
          } else {
            this.$router.push('/')
          }
        })
      }
    }
  }
</script>
<style scoped>
.avatar-uploader-icon {
    width: 180px;
    height: 120px;
    line-height: 120px;
  }
  .avatar {
    width: 180px;
    height: 120px;
  }
</style>
